package com.quanyan.club.task.tracker;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.club.entity.db.TbClub;
import com.quanyan.club.entity.db.TbClubActivity;
import com.quanyan.club.entity.db.TbClubActivityExample;
import com.quanyan.club.entity.db.TbClubMemberApply;
import com.quanyan.club.mapper.*;
import com.quanyan.club.service.ClubActivityService;
import com.quanyan.common.utils.Limit;
import com.quanyan.redisCluster.config.RedisTemplate;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by yue.yuan on 2016/12/21.
 */
@Component
public class ActivitySettleRepeatJob implements JobRunner {

    private static Logger logger = Logger.getLogger(ActivitySettleRepeatJob.class);

    @Autowired
    private ClubActivityService clubActivityService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ClubActivityMapper clubActivityMapper;

    @Autowired
    private TbSignUserMapper tbSignUserMapper;

    @Autowired
    private TbClubActivityMapper tbClubActivityMapper;
    @Autowired
    private ClubActivityCRUDMapper clubActivityCRUDMapper;
    @Autowired
    private TbClubMapper tbClubMapper;


    @Override
    public Result run(JobContext jobContext) throws Throwable {

        logger.warn("开始扫描需要自动结算的活动");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("endTime", new Date());
        int total = clubActivityCRUDMapper.countUnAutoSettleActivity(map);
        logger.warn("扫描个数：" + total);
        Integer pageSize = 20;
        int successCount = 0;
        int all = 0;
        for (int i = 0; i < Math.ceil((double)total/pageSize); i++) {
            int pageNum = i + 1;
            Limit limit = Limit.buildLimit(pageNum, pageSize);
            map.put("pageStart", limit.getStart());
            map.put("pageSize", limit.getSize());
            logger.warn("查询未自动结算的活动：" + JSONObject.toJSONString(map));
            List<TbClubActivity> clubActivityList =
                    clubActivityCRUDMapper.queryUnAutoSettleActivity(map);
            if(CollectionUtils.isEmpty(clubActivityList))
            {
                continue;
            }
            for(TbClubActivity clubActivity : clubActivityList)
            {
                Integer activityId = clubActivity.getActivityId();
                Integer clubId = clubActivity.getClubId();
                TbClub club = tbClubMapper.selectByPrimaryKey(clubId);
                Integer userId = club.getUid();
                if(null == userId || userId == 0)
                {
                    logger.warn("这个活动的创建者不见了！");
                    return new Result(Action.EXECUTE_SUCCESS);
                }
                logger.warn("开始活动补录自动结算，参数：activityId-"
                        + activityId + "clubId:" + clubId + "userId:" + userId);
                try
                {
                    clubActivityService.activitySettlementAuto(activityId, clubId, userId);
                }catch (Exception e)
                {
                    logger.warn(e.getMessage());
                    continue;
                }

            }
        }
        logger.warn("扫描未自动结算活动结束。");
        return new Result(Action.EXECUTE_SUCCESS);
    }
}
